<?php
require_once(APPPATH.'views/components/page.php');
require_once(APPPATH.'libraries/specialmarkobject.php');
require_once (APPPATH.'libraries/paginationobject.php');

/**
 * Special Mark controller
 */
class Mark extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('specialmark_model');
		$this->load->model('product_model');
	}
	
	public function index()
	{
		if (Page::getInstance()->isAdmin())
		{
			$data['marks'] = $this->specialmark_model->getAllSpecialMarks();
			$this->load->view("specialmarks/specialmark_admin_view", $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function insert()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$name = $this->input->post('markname');
			$showall = $this->input->post('showall') != NULL ? TRUE : FALSE;
			
			if ($this->specialmark_model->findSpecialMark($name) < 0)
			{
				$mark = new SpecialMarkObject(-1, $name, $showall);
				$error = $this->specialmark_model->insertSpecialMark($mark);
			}
			else
			{
				$error = EXISTED_ERROR;
				$this->session->set_userdata('markname', $name);
			}
			
			$this->session->set_userdata('error', $error);
			redirect('mark');
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function delete()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$ids = $this->input->post('marks');
			
			$error = $this->specialmark_model->deleteMultipleSpecialMarks($ids);
			
			$this->session->set_userdata('error', $error);
			redirect('mark');
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function view($id)
	{
		if (Page::getInstance()->isAdmin())
		{
			$mark = $this->specialmark_model->getSpecialMarkById($id);
			
			if ($mark->getId() <= 0)
				$data['rerror'] = NOT_EXISTED_ERROR;
			
			$data['mark'] = $mark;
			$this->load->view('specialmarks/specialmark_admin_detail', $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function remove()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$markId = $this->input->post('markid');
			$pids = $this->input->post('products');
			
			$error = $this->specialmark_model->deleteMultipleSpecialMarkProduct($markId, $pids);
			
			$this->session->set_userdata('error', $error);
			redirect('mark/viewproducts/'.$markId);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function update()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$name = $this->input->post('markname');
			$id = $this->input->post('markid');
			$showall = $this->input->post('showall') != NULL ? TRUE : FALSE;
			$mark = $this->specialmark_model->getSpecialMarkById($id);
			
			$foundId = $this->specialmark_model->findSpecialMark($name);
			$existed = FALSE;
			
			if ($foundId == $id)
				$existed = FALSE;
			else {
				if ($foundId > 0)
					$existed = TRUE;
			}
		
			if (! $existed)
			{
				$mark->setTitle($name);
				$mark->setShowAll($showall);
				$error = $this->specialmark_model->updateSpecialMark($id, $mark);
			}
			else
			{
				$error = EXISTED_ERROR;
				$this->session->set_userdata('markname', $name);
			}
			
			$this->session->set_userdata('error', $error);
			
			if ($error == ERROR_NONE)
			{
				redirect('mark');
			}
			else {
				redirect('mark/view/'.$id);
			}
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function viewProducts($id, $curPage = 0)
	{
		if (Page::getInstance()->isAdmin())
		{
			$rec_limit = REC_LIMIT;
			
			if ($curPage == 0)
			{
				$offset = 0;
				$page = 0;
			}
			else {
				$page = $curPage - 1;
				$offset = $rec_limit * $page;
			}
			
			$total = $this->product_model->countProductsBySpecialMarkId($id);
			$paginationLinks = $this->getPaginationLinks('mark/viewproducts/'.$id, $total, $curPage, 4);
			
			$products = $this->product_model->getProductsBySpecialMarkId($id, $offset, $rec_limit);
			
			$data['products'] = $products;
			$data['pagination'] = $paginationLinks;
			$mark = $this->specialmark_model->getSpecialMarkById($id);
			
			if ($mark->getId() <= 0)
				$data['rerror'] = NOT_EXISTED_ERROR;
			
			$data['mark'] = $mark;
			$this->load->view('specialmarks/specialmark_products_view', $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	private function getPaginationLinks($url, $total, $curPage, $segment)
	{
		$config = PaginationObject::getInstance()->createPaginationConfig($url, $total, $curPage, $segment);
		$this->pagination->initialize($config);
		
		return $this->pagination->create_links();
	}
	
	public function addProducts($markId, $curPage = 0)
	{
		if (Page::getInstance()->isAdmin())
		{
			$rec_limit = REC_LIMIT;
			
			if ($curPage == 0)
			{
				$offset = 0;
				$page = 0;
			}
			else {
				$page = $curPage - 1;
				$offset = $rec_limit * $page;
			}
			$products = $this->product_model->getProductsNotHaveMark($markId, $offset, $rec_limit);
			$total = $this->product_model->countProductsNotHaveMark($markId);
			$paginationLinks = $this->getPaginationLinks('mark/addproducts/'.$markId, $total, $curPage, 4);
			
			$data['products'] = $products;
			$data['pagination'] = $paginationLinks;
			$mark = $this->specialmark_model->getSpecialMarkById($markId);
			
			if ($mark->getId() <= 0)
				$data['rerror'] = NOT_EXISTED_ERROR;
			
			$data['mark'] = $mark;
			$this->load->view('specialmarks/specialmark_add_products_view', $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function processAddProducts()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			
			$productIds = $this->input->post('products');
			$markId = $this->input->post('markid');
			
			$error = $this->specialmark_model->addProductsToMark($productIds, $markId);
			
			if ($error != ERROR_NONE)
			{
				$this->session->set_userdata('error', $error);
				redirect('mark/addproducts/'.$markId);
			}
			else
			{
				redirect('mark/viewproducts/'.$markId);
			}
			
		}
		else
		{
			redirect(site_url());
		}
	}
}
/* End of file mark.php */
/* Location: ./application/controllers/mark.php */